<?php
namespace Admin\Model;
use Think\Model;
class RightModel extends Model {
	protected $m_conf = XML_RIGHT;

	/**
	 * 添加权限
	 *
	 * @param string $name    权限名称
	 * @param string $code    权限代码
	 * @param string $info    备注信息
	 * @param string $content 权限序列化内容
	 */
	public function add_right( $name, $code, $info, $content )
	{
		$r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
		$ditem = array();
		$wheres = array();
		$wheres['code'] = array('eq', $code );
		if( !$name  || !$code ){
			$r['i'] = "权限名称获取代码不能为空";
		}
		elseif( $this->where( $wheres )->find() ){
			$r['i'] = "权限代码已经存在";
		}
		else{
			$ditem['right_name'] = $name;
			$ditem['code'] = $code;
			$ditem['info'] = $info;
			$ditem['right_content'] = $content;
			$r['d'] = $this->data( $ditem )->add();
			if( $r['d'] ){
				$r['r'] = 1;
				$r['i'] = 'OK';
			}
			else{
				$r['i'] = '添加权限失败,错误信息'.$this->getError();
			}
		}
		return $r;
	}

	/**
	 * 编辑权限信息
	 *
	 * @param  integer $right_id  权限ID
	 * @param  string  $name       权限名称
	 * @param  string  $code       权限代码
	 * @param  string  $info       备注信息
	 * @param  string  $content    权限序列化内容
	 * @return array   mixed
	 */
	public function edit_right($right_id, $name, $code, $info, $content )
	{
		$r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
		$ditem = array();
		$wheres = array();
		$wheres['right_id'] = array('eq', $right_id);
		$dfind = $this->where( $wheres )->find();
		if( !$dfind ){
			$r['i'] = L("NO_DATA");
		}
		else{
			$wheres['right_id'] = array('neq', $right_id );
			$wheres['code'] = array('eq', $code );
			if( !$name  || !$code ){
				$r['i'] = "权限名称获取代码不能为空";
			}
			elseif( $this->where( $wheres )->find() ){
				$r['i'] = "权限代码已经存在";
			}
			else{
				$ditem['right_name'] = $name;
				$ditem['code'] = $code;
				$ditem['info'] = $info;
				$ditem['right_content'] = $content;
				$wheres['right_id'] = array('eq', $right_id);
				$r['d'] = $this->where( $wheres )->data( $ditem )->save();

				if( $r['d'] ){
					$r['r'] = 1;
					$r['i'] = 'OK';
				}
				else{
					$r['i'] = '修改权限失败,错误信息'.$this->getError();
				}
			}
		}
		return $r;
	}

	/**
	 * 删除权限集合
	 *
	 * @param  integet $id    权限ID
	 * @return array  mixed
	 */
	public function del_right( $id )
	{
		$r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
		$wheres = array();
		$wheres['right_id'] = array('eq', $id );
		if( $this->where( $wheres )->find() && $id ){
			$MR = M("RoleRights");
			$cnt = $MR->where( $wheres )->count();
			if( $cnt ){
				$r['i'] = "存在绑定该权限的岗位";
			}
			else{
				$this->where( $wheres )->delete();
				$r['r'] = 1;
				$r['d'] = 1;
				$r['i'] = "OK";
			}
		}
		else{
			$r['i'] = "删除的对象不存在";
		}

		return $r;
	}

	/**
	 * 获取配置的权限XML内容
	 *
	 * @param  string $conf  XML配置文件位置
	 * @return array
	 */
	public function xml2arr( $conf = '')
	{
		$arr = array();
		if( '' == $conf)
			$conf = $this->m_conf;
		$conf = realpath($conf);
	    if (file_exists( $conf)) {
	      $strRights = file_get_contents($conf);
	      $xml = simplexml_load_string(  $strRights );
	      if( $xml  === FALSE ){
	      	return FALSE;
	      }

	      $common = array();
	      $config = array();

	     foreach($xml->common->children() as $_v){
		     $vk = $_v->attributes()->key->__tostring();
	         $common[$vk]['name'] = $_v->attributes()->name->__tostring();
		     $common[$vk]['child'] = array();
		   foreach( $_v->right as $_vv){
		     $rd = array();
		     $rd['code'] = $_vv->code->__tostring();
		     $rd['name'] = $_vv->name->__tostring();
		     $rd['action'] = $_vv->action->__tostring();
		     $rd['model'] = $_vv->model->__tostring();
			 $common[$vk]['child'][] = $rd;
		    }
	     }

	   foreach($xml->config->children() as $_v){

		   $vk = $_v->attributes()->key->__tostring();
	       $config[$vk]['name'] = $_v->attributes()->name->__tostring();
		   $config[$vk]['child'] = array();
		   foreach( $_v->right as $_vv){
		     $rd = array();
		     $rd['code'] = $_vv->code->__tostring();
		     $rd['name'] = $_vv->name->__tostring();
		     $rd['action'] = $_vv->action->__tostring();
		     $rd['model'] = $_vv->model->__tostring();
			 $config[$vk]['child'][] = $rd;
		   }
	      }
		  $arr['common'] = $common;
		  $arr['config'] = $config;
		}
		return $arr;
	  }
}